<?php

/**
 * ECSHOP短信接口类
 **/
final class ecsms
{
    private $api_url=array(
                        'register' => 'http://sms.ecshop.com/register.php',
                        'auth' => 'http://sms.ecshop.com/user_auth.php',
                        'send' => 'http://idx.sms.shopex.cn/service.php ',
                        'balance' => 'http://sms.ecshop.com/get_balance.php',
                        'history' => 'http://sms.ecshop.com/send_history.php',
                        'bill' => 'http://sms.ecshop.com/charge_history.php'
                    );
    /**
     * ECSHOP短信配置
     **/
    private $setting;
    /**
     * 构造函数
     * @param	string/array	$setting	ECSHOP配置，或配置名
     *          string          读取 ecsms.config.php 里名称的配置
     *          array(
     *              'certi_id'  证书id
     *              'token'     证书
     *              'version'   版本号
     *          )
     * @return  void
     */
    public function __construct($setting = array())
    {
        $this->setting = $setting;
    }
    /**
     * 注册短信服务
     * @param   array   $data   注册短信服务需要数据
     * @return  bool/array      成功失败，注册信息
     **/
    public function register($data = array())
    {

    }
    /**
     * 验证是否已经注册短信服务及会员信息
     * @param   array   $data   验证数据
     * @return  bool/array
     **/
    public function wohis($data = array())
    {

    }
    /**
     * 发送手机短信
     * @param   string  $phone      手机号码，多个用逗号分隔
     * @param   string  $msg        消息内容
     * @param   int     $send_num   发送条数
     * @return  bool                成功/失败
     **/
    public function send($phone, $msg, $send_num = 1)
    {
        if (empty($phone) || empty($msg))
        {
            return false;
        }
        $submit_str=array();
        $submit_str['certi_id'] = $this->setting['certi_id'];
        $submit_str['ac'] = md5($this->setting['certi_id'] . $this->setting['token']);
        $submit_str['version'] = $this->setting['version'];
        $response = $this->setting['http']->post($this->api_url['send'], $submit_str);
        $response = explode("\n", $response);
        foreach ($response as $v)
        {
            preg_match("/[|](http:\/\/.*)[|]/", $v, $matches);
            if ($url = $matches[1])
            {
                break;
            }
        }
        if (empty($url))
        {
            debug::msg("get send sms url false", 1);
        }
        $send_arr = array(0 => array(0 => $phone, 1 => $msg,2 => 'Now'));
        $send_str=array();
        $send_str['certi_id']=$this->setting['certi_id'];
        $send_str['ex_type'] = $send_num;
        $send_str['content'] = json_encode($send_arr);
        $send_str['encoding'] = 'utf8';
        $send_str['version'] = $this->setting['version'];
        $send_str['ac'] = md5($send_str['certi_id'].$send_str['ex_type'].$send_str['content'].$send_str['encoding'].$this->setting['token']);
        $response = $this->setting['http']->post($url, $send_str);
        if(!strstr($response,"true")){
            return false;
        }else{
            return true;
        }
    }
    /**
     * 历史短信发送记录
     * @param   data    $start  开始时间 (留空表示最初)
     * @param   data    $end    结束时间 (留空表示当前)
     * @param   int     $page   页码
     * @param   int     $limit  每页显示数量
     * @return  array
     **/
    public function history($start, $end = '', $page = 1, $limit = 0)
    {

    }
    /**
     * 历史计费记录
     * @param   data    $start  开始时间 (留空表示最初)
     * @param   data    $end    结束时间 (留空表示当前)
     * @param   int     $page   页码
     * @param   int     $limit  每页显示数量
     * @return  array
     **/
    public function bill($start, $end = '', $page = 1, $limit = 0)
    {

    }
}

?>